package com.moon.game.server;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.moon.game.command.Command;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.manager.PlayerMG;
import com.moon.game.message.ObjMessage;
import com.moon.game.server.SessionPool.UniData;
import com.moon.game.util.CommandSet;
import com.moon.game.util.GDateUtils;

public class LogicThread extends Thread{
	
	private Logger log = Logger.getLogger(LogicThread.class);
	 /**
     * 执行Command
     * 
     * @param uniData
     */
    private void execCommand(UniData uniData) {
        if (uniData.from == UniData.FROM_CLIENT) {// 如果是来自客户端的请求,生成一个新的session进行处理
        	uniData.sessionId = SessionPool.inst.getSessionId();
        	RequestSession session = new RequestSession(uniData.sessionId, uniData);
            Command cmd = getCommand(session);
            if (cmd != null) {
                log.info("start session:" + session);
                SessionPool.inst.addReqSession(session);
                cmd.execute(session);
            }

            ObjMessage om = (ObjMessage) (uniData.data);
            if (om != null && PlayerMG.instance.checkLock(om.getPid())) {
                PlayerMG.instance.unLock(om.getPid());
            }
        } else {
            RequestSession session = SessionPool.inst.getReqSession(uniData.sessionId);
            if (session == null) {// session不存在,或者超时
                log.error("ERROR: req session is invalid: sessionId=" + uniData.sessionId);
                return;
            }
            session.setTempData(Constants.PAR_PB_MESSAGE, uniData.data);
            Command cmd = getCommand(session);
            if (cmd != null) {
                cmd.execute(session);
            }
        }
    }
	private Command getCommand(RequestSession session) {
        Command cmd = CommandSet.inst.getCommand(session.getCmd());
        if (cmd != null) {
            return cmd;
        }
		return null;
    }
	@Override
	public void run() {
		while (true) {
			UniData data = null;
			try {
				LinkedBlockingQueue<UniData> exchangeQueue = SessionPool.inst.getExchangeQueue();
				data = exchangeQueue.poll(100, TimeUnit.MILLISECONDS);
				while (data != null) {
					long start = GDateUtils.getMillisNow();
                    execCommand(data);
                    long end = GDateUtils.getMillisNow();
                    // 对处理时间>200毫秒的数据进行监控
                    long interval = end - start;
                    if (interval > 200) {
                        if (data.from == UniData.FROM_CLIENT) {
                            ObjMessage om = (ObjMessage) (data.data);
                            log.warn("poll:" + om + ",use=" + (interval));
                        } else {
                            RequestSession session = SessionPool.inst.getReqSession(data.sessionId);
                            if (session == null) {
                                log.warn("poll: sessionid is invalid: sessionId=" + data.sessionId + ",use=" + (interval));
                            } else {
                                log.warn("poll:" + session + ",use=" + (interval));
                            }
                        }
                    }
                    // 执行队列中的下个数据
                    data = exchangeQueue.poll();
				}
			} catch (Throwable e) {
				log.error("execute command throwable:" + e.getMessage(), e);
			}
		}
	}
}
